//BV technical team
// define global var as handle to API object
var bvSCORM_API = null;
// BVScorm_getAPI, which calls findAPI as needed

function BVScorm_getAPI(){	
  var myAPI = null;
  var tries = 0, triesMax = 10;  
  while (tries < triesMax && myAPI == null){
    window.status = 'Looking for API object ' + tries + '/' + triesMax;
    myAPI = findAPI(window);
    if (myAPI == null && typeof(window.parent) != 'undefined') myAPI = findAPI(window.parent)
    if (myAPI == null && typeof(window.top) != 'undefined') myAPI = findAPI(window.top);
    if (myAPI == null && typeof(window.opener) != 'undefined') if (window.opener != null && !window.opener.closed) myAPI = findAPI(window.opener);      
    if (myAPI == null && typeof(window.opener) != 'undefined'){
	    if (myAPI == null && typeof(window.opener.top) != 'undefined'){
		if (window.opener.top != null && !window.opener.top.closed){
			myAPI = findAPI(window.opener.top);
		}
		
		if (myAPI == null && window.opener.top.opener != null && !window.opener.top.opener.closed){
			myAPI = findAPI(window.opener.top.opener);			
			
			if (myAPI == null && window.opener.top.opener.top != null && !window.opener.top.opener.top.closed){
				myAPI = findAPI(window.opener.top.opener.top);
			}			
		}	
	    }
    }
    tries++;
  }
  
  //alert(myAPI)
  if (myAPI == null){
    window.status = 'API not found';
    //alert('JavaScript Warning: API object not found in window or opener. (' + tries + ')');
  }
  else{
    bvSCORM_API = myAPI;
    window.status = 'API found';
  }
}
// returns LMS API object (or null if not found)
function findAPI(win){
  // look in this window
  if (typeof(win) != 'undefined' ? typeof(win.API) != 'undefined' : false){
    if (win.API != null )  return win.API;
  }
  // look in this window's frameset kin (except opener)
  if (win.frames.length > 0)  for (var i = 0 ; i < win.frames.length ; i++);{
    if (typeof(win.frames[i]) != 'undefined' ? typeof(win.frames[i].API) != 'undefined' : false){
       if (win.frames[i].API != null)  return win.frames[i].API;
    }
  }
  return null;
}
function BVScorm_adlOnload(){
  if (bvSCORM_API != null){  
    var res = bvSCORM_API.LMSInitialize("");
    BVScorm_incomplete();
    //return res;
  }
  //fStartPlayer();
}

function BVScorm_adlOnunload(){
 
  if (bvSCORM_API != null){ 
   // alert("finishing");
    var res = bvSCORM_API.LMSFinish("");
    //return res;
  }
}
function BVScorm_isAPI(){
	if(bvSCORM_API != null){
		return true
	}else{
		return false;
	}
	
}
function BVScorm_getstudentname(){
  if(bvSCORM_API != null){        
    var res = bvSCORM_API.LMSGetValue("cmi.core.student_name")
    return res;
  }
  else
  {
  	return "";
  }
}
function BVScorm_getlocation(){
  if(bvSCORM_API != null){        
    var res = bvSCORM_API.LMSGetValue("cmi.core.lesson_location");
    return res;
  }
  else
  {
  	return "";
  }
}
function BVScorm_setlocation(lPageNum){
//alert("Lesson_location="+lPageNum);
  if(bvSCORM_API != null){        
    var res = bvSCORM_API.LMSSetValue("cmi.core.lesson_location", lPageNum);
    BVScorm_commit()
        //return res;
  }
}

function BVScorm_complete(){
  if(bvSCORM_API != null){        
    var res = bvSCORM_API.LMSSetValue("cmi.core.lesson_status", "completed");
   
    BVScorm_commit()
        //return res;
  }
}
function BVScorm_incomplete(){
 
    if(bvSCORM_API != null){          
        if(bvSCORM_API.LMSGetValue("cmi.core.lesson_status") == "not attempted")
          var res = bvSCORM_API.LMSSetValue("cmi.core.lesson_status", "incomplete");
          BVScorm_commit()
        //return res;
    }
}
function BVScorm_score(score){
  //alert(score)
  if(bvSCORM_API != null ){
    bvSCORM_API.LMSSetValue("cmi.core.score.raw", score);
        //return res;
  }
}
function BVScorm_setcoredata(args){
	if(bvSCORM_API != null){
		var res = bvSCORM_API.LMSSetValue("cmi.suspend_data", args);
		BVScorm_commit()
        //return res;
	}
}
function BVScorm_getcoredata(){
	if(bvSCORM_API != null){
		
        var res = bvSCORM_API.LMSGetValue("cmi.suspend_data");
        return res;
	}
	else
	{
		return "";
	}
}

function BVScorm_commit(){
	if(bvSCORM_API != null){
		var res = bvSCORM_API.LMSCommit("");
	}
	
}
// get the API
BVScorm_getAPI();
